var print_tags = [];
var print_codes =[];
var print_index = 0;
var print_type = 1;
var print_num = 1;
var print_count = 0;
var mode_type = 0;
var label_tamp = "";
var sid = 0;
var keylabel = {};
function init_qrcode(lb) {
  $("#print_qrcode").html('<div class="qrcode1" id="qrcode1"></div>');
  var qrcode = new QRCode(document.getElementById("qrcode1"), {
    width: 250,
    height: 250,
    colorDark: "#000000",
    colorLight: "#ffffff",
    correctLevel: QRCode.CorrectLevel.H
  });
  qrcode.makeCode(lb.LabelNum);


}
function init_barcode() {
  JsBarcode("#barcode", "PR-2022-192019013", { height: 100, displayValue: false });
}
function printLabels(){
  userinfo = get_user_info();
  let printIp = "";
  let labeltamp="";
  let param = get_prefs_data("dyparam");
  console.log(JSON.stringify(param),22222)
  console.log(modeldayin,33333)
  console.log(JSON.stringify(print_type),8888888)
  console.log(JSON.stringify(print_tags),66666)
  console.log(JSON.stringify(mode_type),77777)
  print_name = print_tags[0].print_name + "_"
  switch(print_type){
    case 1:
      printIp = param.ip1
      if(modeldayin == "zebra"){
        printIp = param.mac
      }else if(modeldayin == "tsc"){
        printIp = param.ip1
      }else if(modeldayin == "arco" ){
        printIp = param.arcoip1
      }
      if(param.carton_label_tamp == "mylabel2"){
        labeltamp = print_name + "60x40"
      }else if(param.carton_label_tamp == "mylabel3"){
        labeltamp = print_name + "kateeva"
      }else{
        labeltamp = print_name + "arco"
      }
      break
    case 2:
      printIp = param.ip2
      if(modeldayin == "zebra"){
        printIp = param.mac2
      }else if(modeldayin == "tsc"){
        printIp = param.ip2
      }else if(modeldayin == "arco" ){
        printIp = param.arcoip2
      }
      if(param.pallet_label_tamp == "mylabel1"){
        labeltamp = print_name + "100x140"
       
      }
      break
    case 3:
      printIp = param.ip3
      if(modeldayin == "zebra"){
        printIp = param.mac3
      }else if(modeldayin == "tsc"){
        printIp = param.ip3
      }else if(modeldayin == "arco" ){
        printIp = param.arcoip3
      }
      if(param.sn_label_tamp == "mylabel4"){
        labeltamp = print_name + "arco-97x47"
      }
      break
    default:
      // do something
      break;
    
  }
  
  // console.log(JSON.stringify(api.pageParam),11111)
  // console.log(print_num,333333)
  // console.log(JSON.stringify(print_tags),44444)
 
    let data ={
      "CompanyID": userinfo.Company,
      "PlantID": userinfo.Site,
      "UserName":userinfo.Name,
      "Password":userinfo.password,
      "Printer":printIp,
      "Template":labeltamp,		
      "PrintNum": print_num,
      "Data":JSON.stringify(print_tags)
    }
    console.log(JSON.stringify(data),55555)
    api.ajax({
      url: wmsserver + 'printlabels/',
      method: 'post',
      returnAll: false,
      dataType: 'text',
      headers: {
          'token': wmstoken,
          'Content-Type': 'application/json;charset=utf-8'
      },
      data: {
          body: create_submit_data(data)
      }
    }, function(ret, err){
      console.log(JSON.stringify(err),11111)
      console.log(JSON.stringify(ret),22222)
      let jsonResponse = JSON.parse(ret);
      console.log(JSON.stringify(jsonResponse),333333)
      if(jsonResponse && jsonResponse.data === "200"){ 
        api.alert({
            title: lan['Tips'],
            msg: lan['Transfer_placeholder'] + '/WMSFiles/' + printIp + '/' + labeltamp,
        }, function (ret, err) {
          hideProgress(); 
        });
      }
      
    })
}

//打印箱码
function startPrint() {
  print_type = parseInt(api.pageParam.type);
  print_codes = api.pageParam.codes;
  var lb = print_tags[print_index];
  if(lb.TranType == "PUR-STK"){
    api.pageParam.carton_type = 1
  }else{
    api.pageParam.carton_type = 2
  }
  var param = get_prefs_data("dyparam");
  console_debug(print_type, param);
  console.log(JSON.stringify(print_type),8888888)
  console.log(JSON.stringify(print_tags),66666)
  console.log(JSON.stringify(mode_type),77777)
  console.log(JSON.stringify(lb),666666)
  console.log(JSON.stringify(param),99999)
  switch (print_type) {
    case 1:
      label_tamp = param.carton_label_tamp;
      if (label_tamp == "mylabel2") {
        $("#print_barcode").html('<svg id="barcode"></svg>');
        $(".mylabel2 .print_ShelfNum_title").html("工单号：");
        $(".mylabel2 .print_ShelfNum").html(lb.JobNum);
        if(api.pageParam.carton_type && api.pageParam.carton_type == 1){
          $(".mylabel2 .print_ShelfNum_title").html("采购单号：");
          $(".mylabel2 .print_ShelfNum").html(lb.PONum);
        } 
        $(".mylabel2 .print_Ium").html(lb.IUM);
        $(".mylabel2 .print_LotNum").html(lb.LotNum);
        $(".mylabel2 .print_PartNum").html(lb.PartNum);
        $(".mylabel2 .print_PackageID").html(lb.PackageID);
        $(".mylabel2 .print_LabelNum").html(lb.LabelNum);
        // // 通过类名选择print_LotNum元素  
        let lotNumElement = document.querySelector(".mylabel2");
        // alert(JSON.stringify(lotNumElement),22222); 
        // // 获取print_LotNum元素的HTML内容  
        let lotNumHtml = lotNumElement.innerHTML;  
        // 打印print_LotNum元素的HTML内容  
        // alert(JSON.stringify(lotNumHtml),33333);
        JsBarcode("#barcode", lb.LabelNum, { height: 100, displayValue: false });
        // alert(JSON.stringify(document.querySelector("#mylabel2").innerHTML),444444); 
      }

      if (label_tamp == "mylabel3") {
          if(mode_type == 4 || mode_type == 5 || mode_type == 6){
            $(".mylabel3 print_qrcode").html('<svg id="barcode4"></svg>');
          }else{
            $(".mylabel3 print_qrcode").html('<svg id="barcode2"></svg>');
          }
          $(".mylabel3 .print_LotNum").html(lb.LotNum);
          $(".mylabel3 .print_Ium").html(lb.IUM);
          $(".mylabel3 .print_PartNum").html(lb.PartNum);
          $(".mylabel3 .print_PartDescription").html(lb.PartDescription);
          $(".mylabel3 .print_LabelNum").html(lb.LabelNum);
          $(".mylabel3 .print_TranQty").html(lb.TranQty);
          $(".mylabel3 .print_CountryCode_c").html(lb.CountryCode_c);
          //不合格标签
          $(".mylabel3 .print_User").html(lb.name);
          $(".mylabel3 .print_Text").html('不合格品标签');
          $(".mylabel3 .print_PartNum").html(lb.PartNum);
          $(".mylabel3 .print_LabelNum").html(lb.LabelNum);
          $(".mylabel3 .print_PartDescription").html(lb.PartDescription);
          $(".mylabel3 .print_Reason").html(lb.Description);
          $(".mylabel3 .print_Date").html(lb.CreateDate);
          $(".mylabel3 .print_DMR_Code").html(lb.DMRNum);
          $(".mylabel3 .print_Number").html(lb.TranQty);

          //DMR 合格 不合格标签

          $(".mylabel3 .print_Ware").html(lb.ToWarehouseCode);
          $(".mylabel3 .print_Bin").html(lb.ToBinNum);
          $(".mylabel3 .print_Remark").html(lb.Remark);

          
          // 通过类名选择print_LotNum元素  
          var lotNumElement = document.querySelector(".mylabel3"); 
          // 获取print_LotNum元素的HTML内容  
          var lotNumHtml = lotNumElement.innerHTML;  
          // 打印print_LotNum元素的HTML内容  
          // console.log(lotNumHtml,444444);
          if(mode_type == 4 || mode_type == 5 || mode_type == 6){
            console.log(lotNumHtml,33333);
            JsBarcode("#barcode4", lb.LabelNum, { height: 50, displayValue: false });
          }else{
            JsBarcode("#barcode2", lb.LabelNum, { height: 80, displayValue: false });
          }
        
      }

      if (label_tamp == "mylabel5") {
        arcro_print_carton(print_type, lb);
      }
      break;
    case 2:
      label_tamp = param.pallet_label_tamp;
      if (label_tamp == "mylabel1") {
        $("#CompanyName").html(userinfo.CompanyName);
        $(".mylabel1 .print_PartNum").html(lb.PartNum);
        $(".mylabel1 .print_JobNum").html(lb.JobNum);
        $(".mylabel1 .print_OrderNum").html(lb.PartDescription);
        $(".mylabel1 .print_TranQty").html(lb.TranQty);
        $(".mylabel1 .print_LotNum").html(lb.LotNum);
        init_qrcode(lb);
        $(".mylabel1 .print_LabelNum").html(lb.LabelNum);
      }
      break;
    case 3:
      label_tamp = param.sn_label_tamp;
      if (label_tamp == "mylabel4") {
        if(print_codes == undefined){
          arcro_print_sn(lb);
        }else{
          arcro_print_no(lb);
        }
        
      }
      break;
    default:
      api.toast({
        msg: "未设置好打印模版",
        location: 'middle'
      });
      break;
  }

  if (label_tamp == "mylabel5" || label_tamp == "mylabel4") {
    return;
  }



  setTimeout(function () {
    shareapp();
  }, 800)

  
}
function arcro_print_no(lb) {
  var acrologozpl = "^GFA,5900,5900,50,R01!R03!:R07!::R0!R0FB!Q01!:Q03!::Q07!:Q0!Q0SFE!P01!P01SFD7!P01SF7!P03!:P06DSF3!P07SFE1!P0TFC1!P0TFC0gOFE!P0TF80FD!O01TF807!O01TF007UFI0gQFE001!O03TF007RFBFK0OFM0SFEJ01!O03TF803SF8K01NFM01RFL03!O07TF803RFEM07MFN07PFCM0!O07SFDC01PFDFCM01MFN03PF8M03!O07TFC01RF8N0MFO0PFN01!O0UF400PFDFO03LFO07NFEO07FD!O0TF9E00QFBO01LFO03NFCO03DB!N01TF9E00PFDEI0IFJ0LFO01NFC001FFEI01E!N01TF8F007PFE007FFEEI07KF007IFE001BMFC00FEFFCI0B!N03TF0F007PF601IFDFC003KF007JF800NFC03FFBFFI06!N03TF0F803OFDA07KFE001KF007JFC00MFBC0LFC003!N03SFE07803OF9E0MF001KF007JFE007LF3C1MF001!N07SFE07803OF1E3MFC00KF007KF007KFE3C7DLF801!N07SFC03C01OF1E7MFE01KF007KF007JFDE3CNFC00!N0TFC03C01NFE1EOF07KF007KF807KFC3DFBLFE00E!N0TFC03E00NFC1PF0LF007KF803KF837NFE007!M01TF803E00NFC1FBNF9LF007KF803JFB83PF003!M01TF803F007MF81WF007KF803KF03PF803!M01TF007F007MF81WF007KF803KF03PF803!M03TF007F007MF81WF007KF803JFE03PFC01!M03TF00FF803MF01WF007KF803JFE03PFC01!M07SFE00BF803MF01WF007KF807JFE03PFE00!M07SFE01FFC01MF01WF007KF007JFE03PFE00!M0TFC01FDC01LFE01WF007KF007JFC03PFE00!M0TFC01DFE00LFE01WF007JFE00KFC03QF00!M0TF803FEE00LFE01WF007JFC00KFC03QF00!L01TF803FFE00LFE01WF007JFI0KFC03QF007!L01TF807IF007KFE01WF007IFC001KFC03QF007!L03TF007IF007KFE01WF007CK03KFC03QF007!L03TF00JF803KFE01WF0078K07KFC03QF007!L07SFE00JF803KFE01WF0078K0LFC03QF007!L07SFE00JFC01KFE01WF0078J01LFC03QF007!L07SFC01JFC01KFE00WF0078J03LFC03QF007!L0TFC01JFE01KFE00WF0078J0MFC01QF007!L0TFC03JFE00KFE00WF0078I03MFC01QF007!K01TF803JFE00KFE00DVF0078007NFC01QF00!K01TF807IFB7007JFE00WF007RFC01QF00!K03TF007JFB007KF00WF007FFBOFE01QF00!K03TF00LF803KF006VF007JFC7LFE00QF00!K03SFEM07803KF007VF007IF007LFE00QF00!K07SFEM07C03KF803VF007FF7003LFE007PF01!K07SFEM03C01KF803VF007IF801MF007PF01!K0TFCM03C01KF801VF007IFC01MF003PF03!K0TFCM01E00KFC01VF007IFC00MF803LFEFFD03!J01TF8M01E00KFC00DNF8LF007IFE007DKF801LFDIF07!J01TF8M01F007JFE007EMF07KF007JF003LFC00LF7IF07!J01TFO0F007JFE003MF603KF007JF803LFC00DMFE70D!J03TF007MF807JFB001MFC00KF007JF801LFE007LF7CF0E!J03TF00NF803KF800MF800KF007JFC00MF003MF8F1!J07SFE00NF803KF8007LF001KF007JFE007LFI0LFE0F3!J07SFE01NFC01KFC003KFC003KF007KF007LF8007KF80F7!J0TFC01NFC01KFEI0KFI07KF007KF003LFC001JFE00!J0TFC01NFE00KFBI01IFCI0LF007KF801LF6I03IF800!J0TF803NFE00KFD8J0F8I01DKF007KFC00MFJ03FJ0!I01TF803OF00KF7CO03LF007KFE00MF8O0!I01TF807OF007IFDFFO07BKF007JFBE007LFEO0!I03TF007OF007JFEF8M01MF007JF7F003DLFN03!I03TF00OFD803LFEM03MF007LF801MFCM07!I07TF00NFEF803MF8L0FFEKF807LFC01LFDFL01!I07TF80PFC01MFEK07NF807LFC00NFCK0!I07gVF7CI03PFEMFEDEOF8I07!I0gMFBgIF7!I0gOFBgHFD!001hRF7NFE!001gOF7SF7!003gNFDgJF7NF7NF7!003gPFBgFD!003!007!:00!:01!::03!:07!:0jXFE::1jXFE:3jXFC:7jXF87jWFE,7jVFE,^FS";
  console.log(JSON.stringify(lb),6666666)
  let SerialNumber = ""
  if(lb.type == 1){
    var message = "^XA\n" +
    "^CF0,60\n" +
    "^BY4,3.0,80\n" +
    "^FO50,50^BCN,80,N,N,N,N^FD" + lb.tag.PartNum + "^FS\n" +
    "^FO730,50" + acrologozpl + "\n" +
    "^CF0,30\n" +
    "^FO50,130^APN,40,40^FDPart Number: " + lb.tag.PartNum + "^FS\n" +
    "^FO50,170^APN,40,40^FDIssue: " + app.version + "^FS\n" +
    "^FO50,210^APN,40,40^FDDescription: " + lb.tag.PartDescription + "^FS\n" +
    "^BY4,3.0,80"
    "^FO50,250^BCN,80,N,N,N,N^FD" + lb.tag.LotNum + "^FS"
    "^FO50,330^APN,40,40^FDLot Num: " + lb.tag.LotNum + "^FS\n" +
    "^FO50,380^APN,40,40^FDProduct Group: " + lb.tag.ProdCode + "^FS\n" +
    "^FO50,420^APN,40,40^FDPO/Line: " + lb.tag.POLine + "^FS\n" +
    "^FO50,460^APN,40,40^FDRcv Date: " + lb.tag.TranDate + "^FS\n" +
    "^FO50,500^APN,40,40^FDExpiry Date: " + lb.tag.ExpiryDate + "^FS\n" +
    "^BY2,3.0,10\n" +
    "^FO820,280^BQN,2,10^FD"+ lb.tag.PartNum + "-" + lb.tag.LotNum + "-" + "-"+SerialNumber+"^FS\n" +
    "^XZ";
  }else{
    var aty  = 1;
    var message = "^XA\n" +
    "^CF0,30\n" +
    "^FO50,140^APN,40,40^FDPart Number: " + lb.PartNum + "^FS\n" +
    "^BY4,3.0,80\n" +
    "^FO50,50^BCN,80,N,N,N,N^FD" + lb.PartNum + "^FS\n" +
    "^FO730,50" + acrologozpl + "\n" +
    "^CF0,30\n" +
    "^FO50,180^APN,40,40^FDSerial Number: " + SerialNumber + "^FS\n" +
    "^FO50,220^APN,40,40^FDDescription: " + lb.tag.PartDescription + "^FS\n" +
    "^FO50,350^APN,40,40^FDLot Num: " +lb.tag.LotNum+ "^FS\n" +
    "^BY4,3.0,80\n"+
   " ^FO50,260^BCN,80,N,N,N,N^FD"+lb.tag.LotNum+"^FS\n"+
    "^FO50,390^APN,40,40^FDJob/WO: " + lb.tag.JobNum  + "^FS\n" +
    "^FO50,430^APN,40,40^FDSO: " + lb.tag.OrderNum + "^FS\n" +
    "^FO50,470^APN,40,40^FDQty: " + aty + "^FS\n" +
    "^BY2,3.0,10\n" +
    "^FO840,290^BQN,2,10^FD" + lb.tag.PartNum + "-" + lb.tag.LotNum + "-" + "-"+SerialNumber+"^FS\n" +
    "^XZ";
  }
  print_count = 0;
  console_debug(sid, message);
  for (var i = 0; i < print_num; i++) {
    socket_print(message);
  }

}
function arcro_print_sn(lb) {
  console.log(JSON.stringify(lb),77777)
  var acrologozpl = "^GFA,5900,5900,50,R01!R03!:R07!::R0!R0FB!Q01!:Q03!::Q07!:Q0!Q0SFE!P01!P01SFD7!P01SF7!P03!:P06DSF3!P07SFE1!P0TFC1!P0TFC0gOFE!P0TF80FD!O01TF807!O01TF007UFI0gQFE001!O03TF007RFBFK0OFM0SFEJ01!O03TF803SF8K01NFM01RFL03!O07TF803RFEM07MFN07PFCM0!O07SFDC01PFDFCM01MFN03PF8M03!O07TFC01RF8N0MFO0PFN01!O0UF400PFDFO03LFO07NFEO07FD!O0TF9E00QFBO01LFO03NFCO03DB!N01TF9E00PFDEI0IFJ0LFO01NFC001FFEI01E!N01TF8F007PFE007FFEEI07KF007IFE001BMFC00FEFFCI0B!N03TF0F007PF601IFDFC003KF007JF800NFC03FFBFFI06!N03TF0F803OFDA07KFE001KF007JFC00MFBC0LFC003!N03SFE07803OF9E0MF001KF007JFE007LF3C1MF001!N07SFE07803OF1E3MFC00KF007KF007KFE3C7DLF801!N07SFC03C01OF1E7MFE01KF007KF007JFDE3CNFC00!N0TFC03C01NFE1EOF07KF007KF807KFC3DFBLFE00E!N0TFC03E00NFC1PF0LF007KF803KF837NFE007!M01TF803E00NFC1FBNF9LF007KF803JFB83PF003!M01TF803F007MF81WF007KF803KF03PF803!M01TF007F007MF81WF007KF803KF03PF803!M03TF007F007MF81WF007KF803JFE03PFC01!M03TF00FF803MF01WF007KF803JFE03PFC01!M07SFE00BF803MF01WF007KF807JFE03PFE00!M07SFE01FFC01MF01WF007KF007JFE03PFE00!M0TFC01FDC01LFE01WF007KF007JFC03PFE00!M0TFC01DFE00LFE01WF007JFE00KFC03QF00!M0TF803FEE00LFE01WF007JFC00KFC03QF00!L01TF803FFE00LFE01WF007JFI0KFC03QF007!L01TF807IF007KFE01WF007IFC001KFC03QF007!L03TF007IF007KFE01WF007CK03KFC03QF007!L03TF00JF803KFE01WF0078K07KFC03QF007!L07SFE00JF803KFE01WF0078K0LFC03QF007!L07SFE00JFC01KFE01WF0078J01LFC03QF007!L07SFC01JFC01KFE00WF0078J03LFC03QF007!L0TFC01JFE01KFE00WF0078J0MFC01QF007!L0TFC03JFE00KFE00WF0078I03MFC01QF007!K01TF803JFE00KFE00DVF0078007NFC01QF00!K01TF807IFB7007JFE00WF007RFC01QF00!K03TF007JFB007KF00WF007FFBOFE01QF00!K03TF00LF803KF006VF007JFC7LFE00QF00!K03SFEM07803KF007VF007IF007LFE00QF00!K07SFEM07C03KF803VF007FF7003LFE007PF01!K07SFEM03C01KF803VF007IF801MF007PF01!K0TFCM03C01KF801VF007IFC01MF003PF03!K0TFCM01E00KFC01VF007IFC00MF803LFEFFD03!J01TF8M01E00KFC00DNF8LF007IFE007DKF801LFDIF07!J01TF8M01F007JFE007EMF07KF007JF003LFC00LF7IF07!J01TFO0F007JFE003MF603KF007JF803LFC00DMFE70D!J03TF007MF807JFB001MFC00KF007JF801LFE007LF7CF0E!J03TF00NF803KF800MF800KF007JFC00MF003MF8F1!J07SFE00NF803KF8007LF001KF007JFE007LFI0LFE0F3!J07SFE01NFC01KFC003KFC003KF007KF007LF8007KF80F7!J0TFC01NFC01KFEI0KFI07KF007KF003LFC001JFE00!J0TFC01NFE00KFBI01IFCI0LF007KF801LF6I03IF800!J0TF803NFE00KFD8J0F8I01DKF007KFC00MFJ03FJ0!I01TF803OF00KF7CO03LF007KFE00MF8O0!I01TF807OF007IFDFFO07BKF007JFBE007LFEO0!I03TF007OF007JFEF8M01MF007JF7F003DLFN03!I03TF00OFD803LFEM03MF007LF801MFCM07!I07TF00NFEF803MF8L0FFEKF807LFC01LFDFL01!I07TF80PFC01MFEK07NF807LFC00NFCK0!I07gVF7CI03PFEMFEDEOF8I07!I0gMFBgIF7!I0gOFBgHFD!001hRF7NFE!001gOF7SF7!003gNFDgJF7NF7NF7!003gPFBgFD!003!007!:00!:01!::03!:07!:0jXFE::1jXFE:3jXFC:7jXF87jWFE,7jVFE,^FS";
  if(lb.type == 1){
    var message = "^XA\n" +
    "^CF0,60\n" +
    "^BY4,3.0,80\n" +
    "^FO50,50^BCN,80,N,N,N,N^FD" + lb.tag.PartNum + "^FS\n" +
    "^FO730,50" + acrologozpl + "\n" +
    "^CF0,30\n" +
    "^FO50,130^APN,40,40^FDPart Number: " + lb.tag.PartNum + "^FS\n" +
    "^FO50,170^APN,40,40^FDIssue: " + app.version + "^FS\n" +
    "^FO50,210^APN,40,40^FDDescription: " + lb.tag.PartDescription + "^FS\n" +
    "^BY4,3.0,80"
    "^FO50,250^BCN,80,N,N,N,N^FD" + lb.tag.LotNum + "^FS"
    "^FO50,330^APN,40,40^FDLot Num: " + lb.tag.LotNum + "^FS\n" +
    "^FO50,380^APN,40,40^FDProduct Group: " + lb.tag.ProdCode + "^FS\n" +
    "^FO50,420^APN,40,40^FDPO/Line: " + lb.tag.POLine + "^FS\n" +
    "^FO50,460^APN,40,40^FDRcv Date: " + lb.tag.TranDate + "^FS\n" +
    "^FO50,500^APN,40,40^FDExpiry Date: " + lb.tag.ExpiryDate + "^FS\n" +
    "^BY2,3.0,10\n" +
    "^FO820,280^BQN,2,10^FD"+ lb.tag.PartNum + "-" + lb.tag.LotNum + "-" + "-"+SerialNumber+"^FS\n" +
    "^XZ";
  }else{
    var aty  = 1;
    var message = "^XA\n" +
    "^CF0,30\n" +
    "^FO50,140^APN,40,40^FDPart Number: " + lb.PartNum + "^FS\n" +
    "^BY4,3.0,80\n" +
    "^FO50,50^BCN,80,N,N,N,N^FD" + lb.PartNum + "^FS\n" +
    "^FO730,50" + acrologozpl + "\n" +
    "^CF0,30\n" +
    "^FO50,180^APN,40,40^FDSerial Number: " + SerialNumber + "^FS\n" +
    "^FO50,220^APN,40,40^FDDescription: " + lb.tag.PartDescription + "^FS\n" +
    "^FO50,350^APN,40,40^FDLot Num: " +lb.tag.LotNum+ "^FS\n" +
    "^BY4,3.0,80\n"+
   " ^FO50,260^BCN,80,N,N,N,N^FD"+lb.tag.LotNum+"^FS\n"+
    "^FO50,390^APN,40,40^FDJob/WO: " + lb.tag.JobNum  + "^FS\n" +
    "^FO50,430^APN,40,40^FDSO: " + lb.tag.OrderNum + "^FS\n" +
    "^FO50,470^APN,40,40^FDQty: " + aty + "^FS\n" +
    "^BY2,3.0,10\n" +
    "^FO840,290^BQN,2,10^FD" + lb.tag.PartNum + "-" + lb.tag.LotNum + "-" + "-"+SerialNumber+"^FS\n" +
    "^XZ";
  }
  print_count = 0;
  console_debug(sid, message);
  for (var i = 0; i < print_num; i++) {
    socket_print(message);
  }
    
  

}

function arcro_print_carton(print_type, lb) {
  var acrologozpl = "^GFA,5900,5900,50,R01!R03!:R07!::R0!R0FB!Q01!:Q03!::Q07!:Q0!Q0SFE!P01!P01SFD7!P01SF7!P03!:P06DSF3!P07SFE1!P0TFC1!P0TFC0gOFE!P0TF80FD!O01TF807!O01TF007UFI0gQFE001!O03TF007RFBFK0OFM0SFEJ01!O03TF803SF8K01NFM01RFL03!O07TF803RFEM07MFN07PFCM0!O07SFDC01PFDFCM01MFN03PF8M03!O07TFC01RF8N0MFO0PFN01!O0UF400PFDFO03LFO07NFEO07FD!O0TF9E00QFBO01LFO03NFCO03DB!N01TF9E00PFDEI0IFJ0LFO01NFC001FFEI01E!N01TF8F007PFE007FFEEI07KF007IFE001BMFC00FEFFCI0B!N03TF0F007PF601IFDFC003KF007JF800NFC03FFBFFI06!N03TF0F803OFDA07KFE001KF007JFC00MFBC0LFC003!N03SFE07803OF9E0MF001KF007JFE007LF3C1MF001!N07SFE07803OF1E3MFC00KF007KF007KFE3C7DLF801!N07SFC03C01OF1E7MFE01KF007KF007JFDE3CNFC00!N0TFC03C01NFE1EOF07KF007KF807KFC3DFBLFE00E!N0TFC03E00NFC1PF0LF007KF803KF837NFE007!M01TF803E00NFC1FBNF9LF007KF803JFB83PF003!M01TF803F007MF81WF007KF803KF03PF803!M01TF007F007MF81WF007KF803KF03PF803!M03TF007F007MF81WF007KF803JFE03PFC01!M03TF00FF803MF01WF007KF803JFE03PFC01!M07SFE00BF803MF01WF007KF807JFE03PFE00!M07SFE01FFC01MF01WF007KF007JFE03PFE00!M0TFC01FDC01LFE01WF007KF007JFC03PFE00!M0TFC01DFE00LFE01WF007JFE00KFC03QF00!M0TF803FEE00LFE01WF007JFC00KFC03QF00!L01TF803FFE00LFE01WF007JFI0KFC03QF007!L01TF807IF007KFE01WF007IFC001KFC03QF007!L03TF007IF007KFE01WF007CK03KFC03QF007!L03TF00JF803KFE01WF0078K07KFC03QF007!L07SFE00JF803KFE01WF0078K0LFC03QF007!L07SFE00JFC01KFE01WF0078J01LFC03QF007!L07SFC01JFC01KFE00WF0078J03LFC03QF007!L0TFC01JFE01KFE00WF0078J0MFC01QF007!L0TFC03JFE00KFE00WF0078I03MFC01QF007!K01TF803JFE00KFE00DVF0078007NFC01QF00!K01TF807IFB7007JFE00WF007RFC01QF00!K03TF007JFB007KF00WF007FFBOFE01QF00!K03TF00LF803KF006VF007JFC7LFE00QF00!K03SFEM07803KF007VF007IF007LFE00QF00!K07SFEM07C03KF803VF007FF7003LFE007PF01!K07SFEM03C01KF803VF007IF801MF007PF01!K0TFCM03C01KF801VF007IFC01MF003PF03!K0TFCM01E00KFC01VF007IFC00MF803LFEFFD03!J01TF8M01E00KFC00DNF8LF007IFE007DKF801LFDIF07!J01TF8M01F007JFE007EMF07KF007JF003LFC00LF7IF07!J01TFO0F007JFE003MF603KF007JF803LFC00DMFE70D!J03TF007MF807JFB001MFC00KF007JF801LFE007LF7CF0E!J03TF00NF803KF800MF800KF007JFC00MF003MF8F1!J07SFE00NF803KF8007LF001KF007JFE007LFI0LFE0F3!J07SFE01NFC01KFC003KFC003KF007KF007LF8007KF80F7!J0TFC01NFC01KFEI0KFI07KF007KF003LFC001JFE00!J0TFC01NFE00KFBI01IFCI0LF007KF801LF6I03IF800!J0TF803NFE00KFD8J0F8I01DKF007KFC00MFJ03FJ0!I01TF803OF00KF7CO03LF007KFE00MF8O0!I01TF807OF007IFDFFO07BKF007JFBE007LFEO0!I03TF007OF007JFEF8M01MF007JF7F003DLFN03!I03TF00OFD803LFEM03MF007LF801MFCM07!I07TF00NFEF803MF8L0FFEKF807LFC01LFDFL01!I07TF80PFC01MFEK07NF807LFC00NFCK0!I07gVF7CI03PFEMFEDEOF8I07!I0gMFBgIF7!I0gOFBgHFD!001hRF7NFE!001gOF7SF7!003gNFDgJF7NF7NF7!003gPFBgFD!003!007!:00!:01!::03!:07!:0jXFE::1jXFE:3jXFC:7jXF87jWFE,7jVFE,^FS";

  var message = "^XA\n" +
    "^CF0,60\n" +
    "^BY4,3.0,80\n" +
    "^FO50,50^BCN,N,80,N,N,N^FD" +  lb.PartNum + "^FS\n" +
    "^FO730,200" + acrologozpl + "\n" +
    "^CF0,30\n" +
    "^FO50,130^APN,40,40^FDPart Number: " + lb.PartNum + "^FS\n" +
    "^FO50,170^APN,40,40^FDIssue: " + app.version + "^FS\n" +
    "^FO50,210^APN,40,40^FDDescription: " + lb.PartDescription + "^FS\n" +
    "^BY4,3.0,80\n"+
    "^FO50,250^BCN,80,N,N,N,N^FD" +  lb.LotNum + "^FS\n"+
    "^FO50,340^APN,40,40^FDLot Num: " + lb.LotNum + "^FS\n" +
    "^FO50,380^APN,40,40^FDProduct Group: " + lb.ProdCode + "^FS\n" +
    "^FO50,420^APN,40,40^FDPO/Line: " + lb.POLine + "^FS\n" +
    "^FO50,460^APN,40,40^FDRcv Date: " + lb.TranDate + "^FS\n" +
    "^FO50,500^APN,40,40^FDExpiry Date: " + lb.ExpiryDate + "^FS\n" +
    "^BY2,3.0,10\n" +
    "^FO840,300^BQN,2,10^FD" + lb.PartNum + "-" + lb.LotNum + "-" +"^FS\n" +
    "^XZ";
  console_debug(sid, message);
  print_count = 0;
  for (var i = 0; i < print_num; i++) {
    socket_print(message);
  }

}

function socket_print(message) {

  
  var socketManager = api.require('socketManager');
  socketManager.write({
    sid: sid,
    data: message
  }, function (ret, err) {
    console_debug(ret, err);
    if (ret.status) {
      print_count++;
      if(print_count<print_num){
         return;
      }
      var ol = print_tags[print_index];
      var key = keylabel['label' + ol.LabelNum];
      if(print_type == 3){
        key = keylabel['label' + ol.SerialNumber];
      }
      
      key = parseInt(key);
      app.$set(app.$data.list1[key], 'is_send', true);
      print_index++;
      if (!print_tags[print_index]) {
        hideProgress();
        api.alert({
          title: 'Tips',
          msg: lan['Successfully_sent'],
        }, function (ret, err) {

        });
      } else {
        startPrint();

      }

    } else {
      hideProgress();
    }
  });
}


function shareapp() {
  $("#" + label_tamp).attr("class", label_tamp + " mylbshow");
  $("#" + label_tamp).trigger("click");
  window.pageYOffset = 0;
  document.documentElement.scrollTop = 0
  document.body.scrollTop = 0

  var option = {};
  if (modeldayin == "tsc") {
    option = {};
  } else if (modeldayin == "zebra") {
    var nl = 2400 / api.screenWidth;
    nl = nl.toFixed(0);
    option = { scale: nl };
  }


  html2canvas(document.querySelector("#" + label_tamp), option).then(function (canvas) {


    var bsimg = convertCanvasToImage(canvas);
    var k = bsimg.replace(/^.*?,/, '');
    var param = {};
    param.bsimg = k;
    $("#" + label_tamp).attr("class", label_tamp);
    var type = print_type == 1?2:1;

    wifiprint(type, k, print_num)


  });



}



function wifiprint(type, imgstr, num) {
  // if(printMode == 2){
  //   printLabels()
  //   return;
  // }

  var md = "moduleTsc";
  if (modeldayin == "tsc") {
    md = "moduleTsc";
  } else if (modeldayin == "zebra") {
    md = "moduleZebra";
  }


  console_debug(md, imgstr);



  var moduleTsc = api.require(md);
  moduleTsc.print({ "type": type, "imgstr": imgstr, "print_num": num }, function (ret, err) {
    console_debug(ret, err);
    if (ret) {
      if (ret.status == 1) {
        var ol = print_tags[print_index];
        var key = keylabel['label' + ol.LabelNum];
        key = parseInt(key);
        app.$set(app.$data.list1[key], 'is_send', true);
        print_index++;
        if (!print_tags[print_index]) {
          hideProgress();
          api.alert({
            title: 'Tips',
            msg: lan['Successfully_sent'],
          }, function (ret, err) {

          });
        } else {
          startPrint();

        }

      } else {
        hideProgress();
        errMassage(ret.msg)
      }
    }
  });
}



function convertCanvasToImage(canvas) {
  var image = new Image();
  image.src = canvas.toDataURL("image/jpg");
  return image.src;
}



function wificonnect(type) {
  mode_type = type
  console.log(mode_type,999999)
  // if(printMode == 2){
  //   printLabels()
  //   return;
  // }
  var param = get_prefs_data("dyparam");
  if (!param) {
    api.toast({
      msg: lan['Printer_Parameters_placeholder'],
      location: 'middle'
    });
    return false;
  }
  print_type = parseInt(api.pageParam.type);
  //alert(modeldayin);
  var mac = "";
  var arcoip = "";
  var arcoport = 0;
  switch (print_type) {
    case 1:
      var jsfun = 'wificonnect("' + param.ip1 + '",' + param.port1 + ');';
      if (modeldayin == "tsc") {
        jsfun = 'wificonnect("' + param.ip1 + '",' + param.port1 + ');';
      } else if (modeldayin == "zebra") {
        jsfun = 'wificonnect2("' + param.mac + '");';
        mac = param.mac;
      } else if (modeldayin == "arco") {
        jsfun = 'wificonnect3("' + param.arcoip1 + '",' + param.arcoport1 + ');';
        arcoip = param.arcoip1;
        arcoport = param.arcoport1;
      }

      break;
    case 2:
      var jsfun = 'wificonnect("' + param.ip2 + '",' + param.port2 + ');';
      if (modeldayin == "tsc") {
        jsfun = 'wificonnect("' + param.ip2 + '",' + param.port2 + ');';
      } else if (modeldayin == "zebra") {
        jsfun = 'wificonnect2("' + param.mac2 + '");';
        mac = param.mac2;
      } else if (modeldayin == "arco") {
        jsfun = 'wificonnect3("' + param.arcoip2 + '",' + param.arcoport2 + ');';
        arcoip = param.arcoip2;
        arcoport = param.arcoport2;
      }
      break;
    case 3:
      var jsfun = 'wificonnect("' + param.ip3 + '",' + param.port3 + ');';
      if (modeldayin == "tsc") {
        jsfun = 'wificonnect("' + param.ip3 + '",' + param.port3 + ');';
      } else if (modeldayin == "zebra") {
        jsfun = 'wificonnect2("' + param.mac3 + '");';
        mac = param.mac3;
      } else if (modeldayin == "arco") {
        jsfun = 'wificonnect3("' + param.arcoip3 + '",' + param.arcoport3 + ');';
        arcoip = param.arcoip3;
        arcoport = param.arcoport3;
      }
      break;
    default:
      // do something
      break;
  }

  console_debug(jsfun, "jsfun");



  if (modeldayin == "tsc") {
    api.execScript({
      name: 'root',
      script: jsfun
    });
  } else if (modeldayin == "zebra") {

    zebra_connect(mac);
  } else if (modeldayin == "arco") {

    arco_connect(arcoip, arcoport);
  }




}
var arco_suc = 0;
function arco_connect(arcoip, arcoport) {
  arco_suc = 0;
  var socketManager = api.require('socketManager');
  socketManager.createSocket({
    host: arcoip,
    port: arcoport
  }, function (ret, err) {
    console_debug(ret, err);
    if (ret && ret.state == 102) {
      if (!arco_suc) {
        console_debug(arco_suc, "arco_connect");
        api.sendEvent({
          name: 'print_connected',
          extra: ret
        });
        arco_suc = 1;
      }

    } else {
      //alert(JSON.stringify(err));
    }
  });
}


function zebra_connect(mac) {
  print_type = parseInt(api.pageParam.type);
  var param = get_prefs_data("dyparam");
  var data = {};
  data.connect_type = param.connect_type;
  data.port = 9001;
  if(print_type == 1 && param.connect_type == 'wifi'){
    data.ip = param.zebra_ip1;
    data.port = param.zebra_port1;
  }
  if(print_type == 2 && param.connect_type == 'wifi'){
    data.ip = param.zebra_ip2;
    data.port = param.zebra_port2;
  }

  if(print_type == 3 && param.connect_type == 'wifi'){
    data.ip = param.zebra_ip2;
    data.port = param.zebra_port2;
  }

  if(print_type == 1 && param.connect_type == 'bluetooth'){
    data.mac = param.mac;
  }
  if(print_type == 2 && param.connect_type == 'bluetooth'){
    data.mac = param.mac2;
  }

  if(print_type == 3 && param.connect_type == 'bluetooth'){
    data.mac = param.mac3;
  }


  //alert(mac);
  var moduleZebra = api.require('moduleZebra');
  moduleZebra.connect(data, function (ret, err) {
    console_debug(ret, err);
    if (ret) {
      if (ret.status == 1) {
        api.sendEvent({
          name: 'print_connected',
          extra: 'update'
        });
      }
      if (ret.status == 2) {
        hideProgress();
        errMassage(ret.msg)
      }
    }
  });



}
